package com.loveyukari.dao.impl;

import com.loveyukari.dao.LiwuDao;
import com.loveyukari.dao.LiwuHistoryDao;
import com.loveyukari.model.Liwu;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;

/**
*
*yangshaohua
*2016年12月13日下午3:49:46
**/

@Repository
public class JdbcLiwuHistoryDao extends BaseDao implements LiwuHistoryDao {

	public void insertLiwu(Liwu liwu) {
		String sql = "insert into liwu_history(uid,gfid,hits,give_time) values(?,?,?,?)";
		this.getJdbcTemplate().update(sql, liwu.getUid(),liwu.getGfid(),liwu.getHits(),liwu.getGiveTime());
	}

	@Override
	public void batchInsertLiwu(List<Liwu> list) {
		String sql = "insert into liwu_history(uid,gfid,hits,give_time) values(?,?,?,?)";
		List<Object[]> params = new ArrayList<>();
		for (Liwu liwu :
				list) {
			Object[] param = new Object[4];
			param[0] = liwu.getUid();
			param[1] = liwu.getGfid();
			param[2] = liwu.getHits();
			param[3] = liwu.getGiveTime();
			params.add(param);
		}
		this.getJdbcTemplate().batchUpdate(sql,params);
	}

	public void updateLiwu(Liwu liwu) {
		String sql = "update liwu_history set hits=?,give_time=? where id = ?";
		this.getJdbcTemplate().update(sql, liwu.getHits(),liwu.getGiveTime(),liwu.getId());
	}

	public Liwu selectLiwuByUidAndGfid(Integer uid, Integer gfid) {
		String sql = "select * from liwu_history where uid=? and gfid = ? order by id desc limit 1";
		List<Liwu> list = this.getJdbcTemplate().query(sql, new LiwuHistoryRowMapper(), uid,gfid);
		if (list == null || list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	public Map<Integer, Map<Integer, Liwu>> selectLiwuByDate(Date date) {
		String sql = "select * from liwu_history where give_time between ? and ? order by give_time";
		Map<Integer, Map<Integer, Liwu>> map = this.getJdbcTemplate().query(sql, new ResultSetExtractor<Map<Integer, Map<Integer, Liwu>>>() {
			@Override
			public Map<Integer, Map<Integer, Liwu>> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
				HashMap<Integer, Map<Integer, Liwu>> result = new HashMap<>();
				while (resultSet.next()) {
					int gfid = resultSet.getInt("gfid");
					int uid = resultSet.getInt("uid");
					Map<Integer, Liwu> liwuMap = result.get(uid);
					if (liwuMap == null) {
						liwuMap = new HashMap<>();
						result.put(uid, liwuMap);
					}
					Liwu liwu = liwuMap.get(gfid);
					if (liwu == null) {
						liwu = new Liwu();
						liwu.setId(resultSet.getInt("id"));
						liwu.setGfid(gfid);
						liwu.setGiveTime(resultSet.getTimestamp("give_time"));
						liwu.setHits(resultSet.getInt("hits"));
						liwu.setUid(uid);
						liwuMap.put(gfid, liwu);
					}
				}
				return result;
			}
		}, DateUtils.addDays(date, -1),date );
		return map;
	}

	@Override
	public Liwu selectLiwuByUidAndGfid(Integer uid, Integer gfid, Date date) {
		String sql = "select * from liwu_history where uid=? and gfid = ? and give_time between ? and ? order by give_time desc limit 1";
		List<Liwu> list = this.getJdbcTemplate().query(sql, new LiwuHistoryRowMapper(), uid,gfid,date, DateUtils.addDays(date,1));
		if (list == null || list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	private class LiwuHistoryRowMapper implements RowMapper<Liwu>{

		public Liwu mapRow(ResultSet rs, int rowNum) throws SQLException {
			Liwu liwu = new Liwu();
			liwu.setId(rs.getInt("id"));
			liwu.setGfid(rs.getInt("gfid"));
			liwu.setGiveTime(rs.getTimestamp("give_time"));
			liwu.setHits(rs.getInt("hits"));
			liwu.setUid(rs.getInt("uid"));
			return liwu;
		}
		
	}

}
